const Controller = require('egg').Controller;

class jwtController extends Controller {

    async doLogin(){
        let user = this.ctx.request.body.user;
        if(user.username === "admin" && user.password === "123"){
            let user_jwt = {username:user.username};
            let token = this.ctx.app.jwt.sign(user,this.app.config.jwt.secret)
            this.ctx.body = {
                code:20000,
                token:token
            }
        }else{
            this.ctx.body = {
                code:40000,
                msg:"验证未通过"
            }
        }
    }

    async getMessage(){
        this.ctx.body = "hello jwt"
    }

    // async index(){
    //     let user = {
    //         username : "xiaoming"
    //     }

    //     let token = this.app.jwt.sign(user,this.app.config.jwt.secret)
    //     // this.ctx.body = token

    //     try{
    //         let decode = this.app.jwt.verify(token,this.app.config.jwt.secret)
    //         this.ctx.body = decode.username
    //     }catch(e){
    //         this.ctx.body = "token未通过验证"
    //     }
    // }
}

module.exports = jwtController